package com.tuling.multidatasource.core;

import com.tuling.multidatasource.dynamicdatasource.MultiDataSourceHolder;
import lombok.extern.slf4j.Slf4j;

/**
 * @Author: lijunwei
 * @Time: 2020/9/12 14:35
 */
@Slf4j
public class RoutingTbStrategy extends AbstractTulingRouting {
    private static final String  ROUTING_DS_STRATEGY_DS_KEY = "dataSource00";


    @Override
    public String calDataSourceKey(String routingFieldValue,String routingField) {
        MultiDataSourceHolder.setDataSourceHolder(ROUTING_DS_STRATEGY_DS_KEY);
        return ROUTING_DS_STRATEGY_DS_KEY;
    }

    @Override
    public String calTableKey(String routingFiled) {
        //前置检查
        Integer routingFiledHashCode =  getRoutingFieldHashCode(routingFiled);

        Integer tbIndex = routingFiledHashCode % getTulingDsRoutingSetProperties().getTableNum();

        String tableSuffix = getFormatTableSuffix(tbIndex);

        MultiDataSourceHolder.setTableIndexHolder(tableSuffix);

        return tableSuffix;
    }
}
